package com.it.recyclerview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;

public class MainActivityLinear extends AppCompatActivity {

    /**
     * 复用视图的控件
     */
    private RecyclerView recyclerView;

    /**
     * 显示的数据
     */
    private ArrayList<String> mDatas;

    /**
     * RecyclerView的适配器
     */
    private MyRecyclerViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //1.找到控件
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        //2.创建布局管理器,LinearLayoutManager代表线性的,也就有垂直和水平方向了
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        //3.给linearLayoutManager设置滑动的方向
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        //4.为recyclerView设置布局管理器
        recyclerView.setLayoutManager(linearLayoutManager);

        initData();//初始化数据
        //3.创建适配器
        adapter = new MyRecyclerViewAdapter(this, mDatas);
        //设置分割线
        recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL_LIST));
        //设置添加,移除item的动画,DefaultItemAnimator为默认的
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        //4.设置适配器
        recyclerView.setAdapter(adapter);

        //添加点击事件
        adapter.setOnItemClickListener(new MyRecyclerViewAdapter.OnRecyclerItemClickListener() {

            @Override
            public void onItemClick(View view, int position) {
                //Toast.makeText(MainActivity.this,"单击了:"+mDatas.get(position),Toast.LENGTH_SHORT).show();
                adapter.addItem(position,"添加的内容");
                Log.i("tag", "onItemClick: "+position);
                Log.i("tag", "集合: "+mDatas.toString());
            }
        });
        //设置长按事件
        adapter.setOnItemLongClickListener(new MyRecyclerViewAdapter.onRecyclerItemLongClickListener() {
            @Override
            public void onItemLongClick(View view, int position) {
                //Toast.makeText(MainActivity.this,"长按了:"+mDatas.get(position),Toast.LENGTH_SHORT).show();
                adapter.removeItem(position);
                Log.i("tag", "onItemLongClick: "+position);
                Log.i("tag", "集合: "+mDatas.toString());
            }
        });

    }

    //初始化数据
    protected void initData(){
        mDatas = new ArrayList<String>();
        for (int i = 'A'; i < 'z'; i++){
            mDatas.add("" + (char) i);
        }
    }

}
